#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import requests
import json, sys
import hashlib
from loguru import logger

logger.add("/var/log/Task/netbox/Tasks.log", rotation="00:00", backtrace=True, diagnose=True, level="DEBUG")


# -------api 获取小程序token----
def get_app_token(app_id, app_secret):
    url = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/'

    post_data = {
        "app_id": app_id,
        "app_secret": app_secret,
    }
    post_header = {
        'Content-Type': 'application/json',
    }

    ret = requests.post(url, data=json.dumps(post_data), headers=post_header)
    logger.info(ret)
    lark_ret = json.loads(ret.text)
    return lark_ret.get('tenant_access_token')


def bot_get_group_list(token):
    url = 'https://open.feishu.cn/open-apis/chat/v4/list?page_size=20'
    header = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token,
    }
    ret = requests.get(url, headers=header)
    return ret.text


def bot_get_group_info(token, chat_id):
    url = 'https://open.feishu.cn/open-apis/chat/v4/info?chat_id=' + chat_id
    header = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token,
    }
    ret = requests.get(url, headers=header)
    return ret.text


def bot_get_user_info(token, openid):
    url = 'https://open.feishu.cn/open-apis/user/v4/info?open_id=' + openid

    header = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token,
    }
    ret = requests.get(url, headers=header)
    return ret.text


# -------获取用户所在群组----
def get_user_group_list(token, user):
    url = 'https://open.feishu.cn/open-apis/user/v4/group_list?page_size=20'
    post_header = {
        'Authorization': 'Bearer ' + token,
        'Content-Type': 'application/json',
    }
    ret = requests.get(url, headers=post_header)
    logger.info(ret)
    lark_ret = json.loads(ret.text)
    return lark_ret.get('tenant_access_token')


# print_r($tenant_access_token);

# -------根据邮箱获取用户id----
def get_email2id(token, email):
    url = 'https://open.feishu.cn/open-apis/user/v4/email2id'
    header = {
        'Authorization': 'Bearer ' + token,
        'Content-Type': 'application/json',

    }
    post = {
        "email": email,
    }
    ret = requests.get(url, data=json.dumps(post), headers=header)
    lark_ret = json.loads(ret.text)
    return lark_ret


# -------获取用户信息----
def get_user_info(token, option, userid):
    url = 'https://open.feishu.cn/open-apis/user/v4/info?' + option + '=' + userid
    post_header = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token
    }
    ret = requests.get(url, headers=post_header)
    lark_ret = json.loads(ret.text)

    return lark_ret


# -------open feishu服务台api 获取服务台列表----
def get_helpdesk_list(token, size, id):
    # id格式：'&page_token='+id
    url = 'https://open.feishu.cn/open-apis/helpdesk/v1/helpdesk/list?page_size=' + size + id
    post_header = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token
    }

    ret = requests.get(url, headers=post_header)
    lark_ret = json.loads(ret.text)

    return lark_ret.get('data').get('helpdesk_ids')


# -------open feishu 服务台api 获取服务台信息----
def get_helpdesk_info(token, id):
    # id格式：'?helpdesk_ids=' + id
    url = 'https://open.feishu.cn/open-apis/helpdesk/v1/helpdesk/batch_get' + id
    header = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token
    }
    ret = requests.get(url, headers=header)
    lark_ret = json.loads(ret.text)
    return lark_ret.get('data').get('helpdesks')


# -------服务台api 测试,非开放平台----
def get_helpdesk_agent_online(id):
    # id：非open feishu id,目前未公开，需找nan.zhou 查询
    key = 'd865fsjsibfwtjdjnxd'
    helpdesk_ids = []
    helpdesk_ids.append(int(id))
    md = hashlib.md5()
    md.update(('%s%s' % (id, key)).encode('utf-8'))
    sign = md.hexdigest()
    url = 'https://www.feishu.cn/saipan/v2/external/userschedule/get-oncall-email'

    post_data = {
        "sign": sign,
        "helpdesk_ids": helpdesk_ids,
    }
    header = {
        'Content-Type': 'application/json',
    }
    ret = requests.post(url, data=json.dumps(post_data), headers=header)
    lark_ret = json.loads(ret.text)
    return lark_ret.get('data').get('agent_emails').get(id)


def get_jira():
    url = 'https://helptest.bytedance.net/rest/api/latest/issue/createmeta'
    header = {
        'Content-Type': 'application/json',
    }
    ret = requests.post(url, data=json.dumps(post_data), headers=header)
    return json.loads(ret.text)
